package com.ybwh.springboot2.util;

import com.google.common.base.Preconditions;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * <p>
 * 日期工具类
 * </p>
 *
 * @author fanbeibei
 * @version V1.0
 * @since 2019/9/18 17:35
 */
public class DateUtil {
    public static final int FIRST_MONTH = 0;
    public static final int LAST_MONTH = 11;

    private DateUtil() {
    }


    /**
     * 获取年份
     *
     * @param date
     * @return
     */
    public static int getYear(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.YEAR);
    }


    /**
     * 获取月份值，1月是0
     *
     * @param date
     * @return
     */
    public static int getMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.MONTH);
    }

    /**
     * 获取某月第一天凌晨零点零时零秒
     *
     * @param year
     * @param month 从0开始
     * @return
     */
    public static Date getFirstTimeOfMonth(int year, int month) {
        Preconditions.checkArgument(month >= FIRST_MONTH && month <= LAST_MONTH,"月份必须是从0到11");

        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month);
        c.set(Calendar.DAY_OF_MONTH, 1);
        //将小时至0
        c.set(Calendar.HOUR_OF_DAY, 0);
        //将分钟至0
        c.set(Calendar.MINUTE, 0);
        //将秒至0
        c.set(Calendar.SECOND, 0);
        //将毫秒至0
        c.set(Calendar.MILLISECOND, 0);
        // 获取本月第一天的时间戳
        return c.getTime();
    }


    /**
     * 获取某月最后一天最后一秒时间
     *
     * @param year
     * @param month 从0开始
     * @return
     */
    public static Date getLastTimeOfMonth(int year, int month) {
        Preconditions.checkArgument(month >= FIRST_MONTH && month <= LAST_MONTH,"月份必须是从0到11");

        if (11 == month) {
            month = 1;
            year += 1;
        } else {
            month += 1;
        }

        Date lastTimeOfNextMonth = getFirstTimeOfMonth(year, month);
        return new Date(lastTimeOfNextMonth.getTime() - 1);
    }


    /**
     * 为销售组额度表规范月份字段格式
     *
     * @param date
     * @return
     */
    public static String getMonthForGroup(Date date) {
        if (date == null) {
            date = new Date();
        }
        return String.format("%1$tY-%1$tm", date);
    }

    /**
     * 为销军团额度表规范月份字段格式
     *
     * @return
     */
    public static String getLastMonthForLegion() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.MONTH, c.get(Calendar.MONTH) - 1);
        Date day = c.getTime();
        return String.format("%1$tY-%1$tm", day);
    }

    public static String getMonthForGroup() {
        return getMonthForGroup(null);
    }

    /**
     * 获取上个月
     *
     * @return
     */
    public static String getLastMonth() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.MONTH, c.get(Calendar.MONTH) - 1);
        Date day = c.getTime();
        String str = new SimpleDateFormat("yyyy-MM-dd").format(day);
        return str;
    }

    public static void main(String[] args) {
        System.out.println(DateFormatUtils.format(getFirstTimeOfMonth(getYear(new Date()), getMonth(new Date())), "yyyy-MM-dd HH:mm:ss"));
        System.out.println(DateFormatUtils.format(getLastTimeOfMonth(getYear(new Date()), getMonth(new Date())), "yyyy-MM-dd HH:mm:ss"));

        System.out.println(getMonthForGroup(new Date()));

        System.out.println(getMonth(new Date()));
        System.out.println(getYear(new Date()));

    }


}
